package br.com.amigooo.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.amigooo.model.Establishment;
import br.com.amigooo.model.Role;
import br.com.amigooo.model.User;
import br.com.amigooo.repository.EstablishmentImpl;
import br.com.amigooo.repository.RoleImpl;
import br.com.amigooo.repository.UserImpl;
import br.com.amigooo.util.HibernateUtil;

/**
 * Servlet implementation class CreateServlet
 */
public class CreateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CreateServlet() {
    	HibernateUtil.getSessionFactory();
        RoleImpl roleImpl = new RoleImpl();
        Role role = roleImpl.getRole("ROLE_ADMIN");
        
        if(role == null){
        	System.out.println("*** Carregando dados");
        	
    		EstablishmentImpl establishmentImpl = new EstablishmentImpl();
    		UserImpl userImpl = new UserImpl();
    		
    		role = new Role();
        	role.setName("ROLE_ADMIN");
        	role.setDescription("Permissões de administrador.");
        	roleImpl.save(role);
        	
        	role = new Role();
        	role.setName("ROLE_COMPANY");
        	role.setDescription("Permissões de estabelecimento.");
        	roleImpl.save(role);
        	
        	role = new Role();
        	role.setName("ROLE_USER");
        	role.setDescription("Permissões de usuário comum.");
        	roleImpl.save(role);
        	
    		Establishment establishment = new Establishment();
    		User user = new User();
    		
    		Set<Role> lRole = new HashSet<Role>();
    		Set<User> lUser = new HashSet<User>();
    		
    		role = roleImpl.getRole("ROLE_ADMIN");
    		user.setActive(true);
    		user.setEmail("machado.rodrigom@gmail.com");
    		user.setName("Rodrigo Machado");
    		user.setRegistration("machado");
    		user.setRating(5);
    		user.setRegistrationDate(new Date());
    		user.setUserPassword("adm");
    		lRole.add(role);
    		user.setRole(lRole);
    		
    		user = userImpl.save(user);
    		
    		establishment.setCompany("Restaurante");
    		establishment.setEmail("mailrodrigo@netscape.net");
    		establishment.setPhone("5555-5555");
    		establishment.setTax("0000000-00001");
    		lUser.add(user);
    		establishment.setUser(lUser);
    		establishment = establishmentImpl.save(establishment);
    		
    		user.setEstablishment(establishment);
    		userImpl.update(user);
        }else{
        	System.out.println("*** Informações já criadas.");
        }
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
